//
//  ProgressViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-7-12.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ProgressViewController.h"


@implementation ProgressViewController

@synthesize uploadController;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	progressTitle = (NSLocalizedStringFromTable(@"UPLOADING_PIC",@"main",@""));
	[btnCancel setTitle:(NSLocalizedStringFromTable(@"CANCEL",@"main",@"")) forState:UIControlStateNormal];
	ch = imgView.frame.size.height;
	cw = imgView.frame.size.width;
	NSString *strLoad = NSLocalizedStringFromTable(@"LOADING",@"main",@"");
	lblTitle.text = strLoad;
	lblResolution.text = strLoad;
	lblDataSize.text = strLoad;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark -
#pragma mark Cancel_Upload

-(IBAction)cancelUploadTask:(id)sender{
	[uploadController stopUpload];
}

/**
 * 设置进度标题，标题部分，只包括图像标题即可，其余的载方法内部完成
 */
-(void) setProgressTitle:(NSString *)title{
	lblProcess.text = [NSString stringWithFormat:@"%@%@",progressTitle,title];
}

/**
 * 设置上传信息，用于显示图像以及各种信息
 */
-(void) setUploadItem:(UploadItem *)upload{
	lblTitle.text = [NSString stringWithFormat:@"%@%@",progressTitle,upload.title];
	
	// TODO 设置图像
	
	UIImage *img = upload.image;
	
	CGFloat iw = img.size.width;
	CGFloat ih = img.size.height;
	
	CGFloat iscal;
	
	if(iw >= ih){
		iscal = cw / iw;
	}else{
		iscal = ch / ih;
	}
	
	imgView.image = img;
	// 使用这种方法可以将图像缩小，但是比例需要检查
	imgView.transform = CGAffineTransformMakeScale(iscal, iscal);
	
	lblResolution.text = [NSString stringWithFormat:@"%@: %dx%d",NSLocalizedStringFromTable(@"RESOLUTION",@"main",@""),(int)iw,(int)ih];
	NSData *data = [upload getImageData:0];
	lblDataSize.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedStringFromTable(@"FILE_SIZE",@"main",@""),[self sizeDisplay:[data length]]];
	
}

/**
 * 文件长度转换显示
 */
-(NSString *)sizeDisplay:(NSInteger)len{
	int m,k,b;
	k = len >> 10;
	m = k >> 10;
	b = len >> 20;
	NSMutableString *ss = [NSMutableString string];
	BOOL bSet = NO;
	if(m > 0){
		[ss appendFormat:@"%d M ",m];
		bSet = YES;
	}
	if(k > 0){
		[ss appendFormat:@"%d K ",k];
		bSet = YES;
	}
	if(!bSet){
		[ss appendFormat:@"%d ",len];
	}
	[ss appendString:@"Bytes"];
	return ss;
}


- (void)dealloc {
    [super dealloc];
}


@end
